/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package foxmob.mlts.view.student;

import foxmob.mlts.model.MultipleFillInBlankQuestion;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 *
 * @author zpf16z
 */
public class MultipleFillInBlanksPanel extends javax.swing.JPanel {
    String question = "This is a __, and I ________ not like _.";
    private ArrayList<String> studentAnswer = new ArrayList<String>();
    private int countAnswer = -1;
    private ArrayList<JTextField> textField = new ArrayList<JTextField>();
    private boolean state = false;
    private MultipleFillInBlankQuestion mfib;

    /**
     * Creates new form MultipleFillInBlanksPanel
     */
    public MultipleFillInBlanksPanel() {
        initComponents();
        setQuestion(question);
        this.jPanelMain.repaint();
    }

    private void setQuestion(String question) {
        char[] q = question.toCharArray();
        int length = question.length();
        for (int i = 0; i < length;) {
            if (i < length && q[i] == '_') {
                countAnswer ++;
                final JTextField tf = new JTextField();
                textField.add(tf);
                studentAnswer.add("0");
                int wide = 0;
                while (q[i] == '_') {
                    wide++;
                    i++;
                }
                tf.setColumns(wide * 10);
                tf.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("Text=" + tf.getText());
                        setAnswer(textField.indexOf(tf), tf.getText());
                    }


                });
                
                Dimension d = new Dimension();
                d.setSize(1, 28);
                tf.setPreferredSize(d);
                tf.setMaximumSize(d);
                this.jPanelMain.add(tf);
                this.jPanelMain.repaint();
            } else {
                JLabel lable = new JLabel();
                int wide = 0;
                String temp = "";
                while (i < length && q[i] != '_') {
                    temp += q[i];
                    wide++;
                    i++;
                }
                if (temp != null) {
                    System.out.println(temp);
                    lable.setText(temp);
                    this.jPanelMain.add(lable);
                }
            }
        }
    }
        private void setAnswer(int i, String answer) {
//        fib = (FillInTheBlanksQuestion) QuestionPaperForm.testRecord.getAllQuestions().get(this.questionCount - 1);
//        fib.setStudentAnswer(this.jTextFieldAnswer.getText());
        studentAnswer.set(i, answer);
        System.out.println(studentAnswer.toString());
        //mfib.setStudentAnswer((String[])studentAnswer.toArray());
        setQuestionFinished();
        //TO do
        // Save answers to File.
    }

    private void setQuestionFinished() {
//        if (this.state) {
//            if (this.jTextFieldAnswer.getText().length() == 0) {
//                this.state = false;
//                QuestionPaperForm.finishedQuestions--;
//            }
//        } else {
//            if (this.jTextFieldAnswer.getText().length() > 0) {   
//                this.state = true;
//                QuestionPaperForm.finishedQuestions++;
//            }
//        }
        this.state = true;
    }

    public void loadQuestion(MultipleFillInBlankQuestion question, int count) {
        this.mfib = question;
        char[] q = question.getQuestionText().toCharArray();
        for (int i = 0; i < q.length;) {
            if (i < q.length && q[i] == '_') {
                countAnswer ++;
                final JTextField tf = new JTextField();
                textField.add(tf);
                studentAnswer.add("0");
                int wide = 0;
                while (q[i] == '_') {
                    wide++;
                    i++;
                }
                tf.setColumns(wide * 10);
                tf.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("Text=" + tf.getText());
                        setAnswer(textField.indexOf(tf), tf.getText());
                    }


                });
                
                Dimension d = new Dimension();
                d.setSize(1, 28);
                tf.setPreferredSize(d);
                tf.setMaximumSize(d);
                this.jPanelMain.add(tf);
                this.jPanelMain.repaint();
            } else {
                JLabel lable = new JLabel();
                int wide = 0;
                String temp = "";
                while (i < q.length && q[i] != '_') {
                    temp += q[i];
                    wide++;
                    i++;
                }
                if (temp != null) {
                    System.out.println(temp);
                    lable.setText(temp);
                    this.jPanelMain.add(lable);
                }
            }
        }
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelMain = new javax.swing.JPanel();
        jLabelQuestionID = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setMaximumSize(new java.awt.Dimension(10, 100));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanelMain.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanelMain.setMaximumSize(new java.awt.Dimension(20, 100));
        jPanelMain.setMinimumSize(new java.awt.Dimension(20, 100));
        jPanelMain.setPreferredSize(new java.awt.Dimension(190, 20));
        jPanelMain.setLayout(new javax.swing.BoxLayout(jPanelMain, javax.swing.BoxLayout.LINE_AXIS));

        jLabelQuestionID.setText("  Q1.");
        jPanelMain.add(jLabelQuestionID);

        add(jPanelMain, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 430, 50));
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabelQuestionID;
    private javax.swing.JPanel jPanelMain;
    // End of variables declaration//GEN-END:variables
}
